<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:asp="remove" xmlns:ctl="remove" xmlns:tinymce="remove" xmlns:ptl="remove">
	<xsl:output method="xml"/>
	<xsl:template match="/">
		<!-- inizio parte dinamica -->
		<div id="custom_form" class="dynform">
			<!-- Titolo del Modulo Form-->
			<div id="testata_custom" class="dfhead">
                  SYS_MODULE_NAME
                </div>
			<div id="corpo_custom" class="dfbody">
				<!-- prima verifica - presenza colonne -->
				<div id="leftc_custom" class="dfmainbody">
					<xsl:call-template name="Colonna">
						<xsl:with-param name="num_colonna">1</xsl:with-param>
					</xsl:call-template>
				</div>
			</div>
			<div id="footer_custom" class="dffoot">SYS_MODULE_FOOTER</div>
		</div>
		<!-- fine parte dinamica   -->
	</xsl:template>
	<!-- Colonna XX-->
	<xsl:template name="Colonna">
		<!-- seleziona quelli dove Accordion=0 e Tag = 0 -->
		<xsl:for-each select="//UserProfile/Campi/Campo">
			<xsl:sort select="number(Ordine)"/>
			<xsl:call-template name="MioCampo">
				<xsl:with-param name="inome" select="Nome"/>
				<xsl:with-param name="itipo" select="Tipo"/>
				<xsl:with-param name="ivalore" select="Valore"/>
				<xsl:with-param name="ilabel" select="Label"/>
				<xsl:with-param name="test_id"></xsl:with-param>
			</xsl:call-template>
		</xsl:for-each>
	</xsl:template>
	
	<!-- Campo INPUT -->
	<xsl:template name="MioCampo">
		<!-- Bisogna inseririre tutta la definizione campi di ASp.NEt -->
		<xsl:param name="inome"/>
		<xsl:param name="itipo"/>
		<xsl:param name="ivalore"/>
		<xsl:param name="ilabel"/>
		<xsl:param name="test_id"/>
		<!-- 
      Campi: ID,       Ordine,       Tipo,    Valore, 
             Required, Regex,        Label,   Nome,
             ReadOnly, Parameters
             TipoDato
    -->
		<xsl:variable name="mioCC_ID">
			<xsl:value-of select="$test_id"/>_<xsl:value-of select="translate(normalize-space(Nome),'_','') "/>
		</xsl:variable>
		<xsl:if test=" (Label/text()) and Tipo!='Label' and Tipo!='Suggestion' ">
			<asp:Label Id="LAB{ normalize-space($mioCC_ID) }" AssociatedControlID="{normalize-space($mioCC_ID)}" Text="{Label}" Runat="server"/>
		</xsl:if>
		<xsl:choose>
			<xsl:when test="$itipo='CheckBox'">
				<asp:CheckBox Id="{normalize-space($mioCC_ID)}" TextAlign="Left" Checked="{Valore}" Runat="server">
					<xsl:if test="ReadOnly/text()">
						<xsl:attribute name="Enabled"><xsl:choose><xsl:when test="ReadOnly='True'">False</xsl:when><xsl:otherwise>True</xsl:otherwise></xsl:choose></xsl:attribute>
					</xsl:if>
				</asp:CheckBox>
			</xsl:when>
			<xsl:when test="$itipo='Label'">
				<asp:Label Id="{normalize-space($mioCC_ID)}" Text="{Valore}" Runat="server"/>
			</xsl:when>
			<xsl:when test="$itipo='RadioButtonList'">
				<asp:RadioButtonList ID="{normalize-space($mioCC_ID)}" runat="server" RepeatLayout="Flow">
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
					<xsl:call-template name="SplitList">
						<xsl:with-param name="strInput" select="Valore"/>
					</xsl:call-template>
				</asp:RadioButtonList>
			</xsl:when>
			<xsl:when test="$itipo='TextBox'">
				<asp:TextBox Id="{normalize-space($mioCC_ID)}" TextMode="SingleLine" Text="{Valore}" Runat="server">
					<xsl:if test="ReadOnly/text()">
						<xsl:attribute name="ReadOnly"><xsl:value-of select="ReadOnly"/></xsl:attribute>
					</xsl:if>
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</asp:TextBox>
			</xsl:when>
			<xsl:when test="$itipo='TextArea'">
				<asp:TextBox Id="{normalize-space($mioCC_ID)}" TextMode="Multiline" Text="{Valore}" Runat="server">
					<xsl:if test="ReadOnly/text()">
						<xsl:attribute name="ReadOnly"><xsl:value-of select="ReadOnly"/></xsl:attribute>
					</xsl:if>
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</asp:TextBox>
			</xsl:when>
			<xsl:when test="$itipo='TextHtml'">
				<tinymce:TextArea id="{normalize-space($mioCC_ID)}" 
                theme="advanced"
                plugins="spellchecker,safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template"
                theme_advanced_buttons1="fullscreen,spellchecker,template,preview,|,bold,italic,underline,strikethrough,|,styleselect,formatselect,fontselect,fontsizeselect"
                theme_advanced_buttons2="cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,code,|,insertdate,inserttime"
                theme_advanced_buttons3="tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr"
                theme_advanced_buttons4="insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,justifyleft,justifycenter,justifyright,justifyfull,|,ltr,rtl,|,forecolor,backcolor"
                theme_advanced_toolbar_location="top"
                theme_advanced_toolbar_align="left"
                theme_advanced_path_location="bottom"
                theme_advanced_resizing="true"
                runat="server">
					<xsl:if test="ReadOnly/text()">
						<xsl:attribute name="ReadOnly"><xsl:value-of select="ReadOnly"/></xsl:attribute>
					</xsl:if>
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</tinymce:TextArea>
			</xsl:when>
            
			<xsl:when test="$itipo='Password'">
				<asp:TextBox Id="{normalize-space($mioCC_ID)}" TextMode="Password" Text="{Valore}" Runat="server">
					<xsl:if test="ReadOnly/text()">
						<xsl:attribute name="ReadOnly"><xsl:value-of select="ReadOnly"/></xsl:attribute>
					</xsl:if>
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</asp:TextBox>
			</xsl:when>
			<!-- esperimento -->
			<xsl:when test="$itipo='FileUpload'">
        <ptl:FileUpload id="{normalize-space($mioCC_ID)}" runat="server"/>
			</xsl:when>
			
			<xsl:when test="$itipo='DropDownList'">
				<asp:DropDownList Id="{normalize-space($mioCC_ID)}" runat="server">
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
					<xsl:call-template name="SplitList">
						<xsl:with-param name="strInput" select="Valore"/>
					</xsl:call-template>
				</asp:DropDownList>
			</xsl:when>
			<xsl:when test="$itipo='SectionChooser'">
				<ctl:DropDownSezioni Id="{normalize-space($mioCC_ID)}" runat="server">
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</ctl:DropDownSezioni>
			</xsl:when>
			<xsl:when test="$itipo='CategoryChooser'">
				<ctl:DropDownCategorie Id="{normalize-space($mioCC_ID)}" runat="server">
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</ctl:DropDownCategorie>
			</xsl:when>
			<xsl:when test="$itipo='ContentChooser'">
				<ctl:DropDownChooseContent Id="{normalize-space($mioCC_ID)}" runat="server">
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</ctl:DropDownChooseContent>
			</xsl:when>
			<xsl:when test="$itipo='LanguageChooser'">
				<ctl:DropDownLanguages Id="{normalize-space($mioCC_ID)}" runat="server">
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</ctl:DropDownLanguages>
			</xsl:when>
			<xsl:when test="$itipo='MenuTree'">
				<ctl:MenuTree Id="{normalize-space($mioCC_ID)}" runat="server">
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</ctl:MenuTree>
			</xsl:when>
			<xsl:when test="$itipo='SelectContent'">
				<ctl:SelectContent Id="{normalize-space($mioCC_ID)}" runat="server">
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</ctl:SelectContent>
			</xsl:when>
			<xsl:when test="$itipo='ViewBlocks'">
				<ctl:DropDownBVisivo Id="{normalize-space($mioCC_ID)}" runat="server">
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</ctl:DropDownBVisivo>
			</xsl:when>
			<xsl:when test="$itipo='SelectTimeZone'">
				<ctl:SelectTimeZone Id="{normalize-space($mioCC_ID)}" runat="server">
					<xsl:if test="Parameters/text()">
						<xsl:call-template name="ReadParameters">
							<xsl:with-param name="strInput" select="Parameters"/>
						</xsl:call-template>
					</xsl:if>
				</ctl:SelectTimeZone>
			</xsl:when>
			<xsl:when test="$itipo='Suggestion'">
				<span class="suggest">
					<span class="title">
						<xsl:value-of select="Label"/>
					</span>
					<xsl:value-of select="Valore" disable-output-escaping="yes">
           </xsl:value-of>
				</span>
			</xsl:when>
			<xsl:when test="$itipo='ColorPicker'">
				<ctl:ColorPicker Id="{normalize-space($mioCC_ID)}" runat="server" color="{valore}">
					
				</ctl:ColorPicker>
			</xsl:when>
			<xsl:when test="$itipo='ComponentChooser'">
				<ctl:ChooseComponentInstance Id="{normalize-space($mioCC_ID)}" runat="server">
					
				</ctl:ChooseComponentInstance>
			</xsl:when>			
			
			
		</xsl:choose>
		<!-- Required :: -->
		<xsl:choose>
			<xsl:when test="$itipo = 'CheckBox' or $itipo = 'Label' or $itipo='Suggestion' ">
     
		    </xsl:when>
			<xsl:otherwise>
				<xsl:if test="Required = 'true' ">
					<asp:RequiredFieldValidator ErrorMessage="Richiesto" runat="server" ControlToValidate="{normalize-space($mioCC_ID)}"/>
				</xsl:if>
			</xsl:otherwise>
		</xsl:choose>
		<!-- Regular Type -->
		<xsl:choose>
			<xsl:when test="$itipo = 'Password' or $itipo = 'TextBox' or $itipo = 'TextArea'">
				<xsl:choose>
					<xsl:when test="TipoDato='Other' or TipoDato='Email'">
						<xsl:if test="Regex/text()">
							<asp:RegularExpressionValidator ErrorMessage="Valore non Valido" ControlToValidate="{normalize-space($mioCC_ID)}" runat="server" ValidationExpression="{normalize-space(Regex)}"/>
						</xsl:if>
					</xsl:when>
					<xsl:otherwise>
						<xsl:if test="TipoDato = 'Currency'">
							<asp:CompareValidator Type="Currency" Operator="DataTypeCheck" ErrorMessage="Inserisci un Importo" runat="server" ControlToValidate="{normalize-space($mioCC_ID)}"/>
						</xsl:if>
						<xsl:if test="TipoDato = 'Date'">
							<asp:CompareValidator Type="Date" Operator="DataTypeCheck" ErrorMessage="Inserisci una Data" runat="server" ControlToValidate="{normalize-space($mioCC_ID)}"/>
						</xsl:if>
						<xsl:if test="TipoDato = 'Double'">
							<asp:CompareValidator Type="Double" Operator="DataTypeCheck" ErrorMessage="Inserisci un Numero Decimale" runat="server" ControlToValidate="{normalize-space($mioCC_ID)}"/>
						</xsl:if>
						<xsl:if test="TipoDato = 'Integer'">
							<asp:CompareValidator Type="Integer" Operator="DataTypeCheck" ErrorMessage="Inserisci un Numero Intero" runat="server" ControlToValidate="{normalize-space($mioCC_ID)}"/>
						</xsl:if>
						<xsl:if test="TipoDato = 'String'">
							<asp:CompareValidator Type="String" Operator="DataTypeCheck" ErrorMessage="PInserisci solo Testo" runat="server" ControlToValidate="{normalize-space($mioCC_ID)}"/>
						</xsl:if>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:when>
			<xsl:otherwise/>
		</xsl:choose>
		<br class="clearbr"/>
	</xsl:template>

	<!-- Spli List per LISTITEM -->
	<xsl:template name="SplitList">
		<!--This template will recursively break apart a comma-delimited string into child elements-->
		<xsl:param name="strInput"/>
		<xsl:param name="strDelimiter" select="'~'"/>
		<xsl:variable name="strNextItem" select="substring-before($strInput,$strDelimiter)"/>
		<xsl:variable name="strOutput" select="substring-after($strInput,$strDelimiter)"/>
		<xsl:variable name="strLen" select="string-length($strNextItem)"/>
		<xsl:choose>
			<xsl:when test="contains($strInput,$strDelimiter)">
				<asp:ListItem>
					<xsl:value-of select="$strNextItem"/>
				</asp:ListItem>
				<!-- At this point, the template will recursively call itself until the last comma is found -->
				<xsl:call-template name="SplitList">
					<xsl:with-param name="strInput" select="$strOutput"/>
					<xsl:with-param name="strDelimiter" select="$strDelimiter"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<!-- The otherwise clause will be reached when a comma is not located using contains() -->
				<asp:ListItem>
					<xsl:value-of select="$strInput"/>
				</asp:ListItem>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!-- Applica lista Parametri -->
	<xsl:template name="ReadParameters">
		<!--Con questo template si tagliano i parametri separati da ~ e si applicano al tag contenitore.-->
		<xsl:param name="strInput"/>
		<xsl:param name="strDelimiter" select="'~'"/>
		<xsl:variable name="strNextItem" select="substring-before($strInput,$strDelimiter)"/>
		<xsl:variable name="strOutput" select="substring-after($strInput,$strDelimiter)"/>
		<xsl:variable name="strLen" select="string-length($strNextItem)"/>
		<xsl:choose>
			<xsl:when test="contains($strInput,$strDelimiter)">
				<!-- applico la prima proprietà che trovo. -->
				<xsl:call-template name="SetCustomParam">
					<xsl:with-param name="strCoppia" select="$strNextItem"/>
				</xsl:call-template>
				<!--
		 <xsl:attribute name="class">
			 <xsl:value-of select="$strNextItem" />
		 </xsl:attribute>
		  -->
				<!-- Chiama ricorsivamente il template finchè non completa le tilde -->
				<xsl:call-template name="ReadParameters">
					<xsl:with-param name="strInput" select="$strOutput"/>
					<xsl:with-param name="strDelimiter" select="$strDelimiter"/>
				</xsl:call-template>
			</xsl:when>
			<xsl:otherwise>
				<!-- se arrivo qui applico la proprietà, quindi splitto su UGUALE e ottengo paramo e valore-->
				<xsl:call-template name="SetCustomParam">
					<xsl:with-param name="strCoppia" select="$strInput"/>
				</xsl:call-template>
				<!--
		 <xsl:attribute name="class">
			 <xsl:value-of select="$strInput"/>
		 </xsl:attribute>
		-->
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!-- setta parametro -->
	<xsl:template name="SetCustomParam">
		<xsl:param name="strCoppia"/>
		<xsl:variable name="cutchar">=</xsl:variable>
		<xsl:variable name="strChiave" select="substring-before($strCoppia,$cutchar)"/>
		<xsl:variable name="strValore" select="substring-after($strCoppia,$cutchar)"/>
		<xsl:choose>
			<xsl:when test="contains($strCoppia,$cutchar)">
				<xsl:attribute name="{$strChiave}"><xsl:value-of select="$strValore"/></xsl:attribute>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>
